package com.chixing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chixing.entity.Dish;
import com.chixing.mapper.DishMapper;
import com.chixing.service.IDishService;
import com.chixing.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author smith
 * @since 2025-05-21
 */

//alt + enter
@Service
public class DishServiceImpl   implements IDishService {
    //调用持久层
    @Autowired
    private DishMapper dishMapper;

    @Override
    public Result getByTypeId(Integer typeId) {
        QueryWrapper<Dish> wrapper = new QueryWrapper<>();
        wrapper.eq("type_id",typeId);
        List<Dish> dishList = dishMapper.selectList(wrapper);
        if(dishList !=null && dishList.size() >0)
            return Result.getSuccess(dishList);
        else
            return Result.getFail("今はまだこのタイプの商品がありません");
    }

    @Override
    public Result getById(Integer dishId) {
        //select * from dish where dish_id = 4
        Dish dish = dishMapper.selectById(dishId);
        if(dish != null)
            return Result.getSuccess(dish);
        return Result.getFail(null);
    }

    @Override
    public Result getAll() {
        QueryWrapper<Dish> wrapper = new QueryWrapper<>();
        wrapper.ne("type_id", 8); // 不等于 typeId = 8
        List<Dish> dishList = dishMapper.selectList(wrapper);
        if (dishList != null && !dishList.isEmpty())
            return Result.getSuccess(dishList);
        else
            return Result.getFail("今はまだ無いです,");
    }

    @Override
    public Result save(Dish dish) {
        // insert into dish ....
        // rows 表示 dish表中受影响行数 , rows = 1 表示 添加了一条记录成功
        int rows = dishMapper.insert(dish);
        if(rows >0)
            return Result.saveSuccess(dish);
        return Result.saveFail(dish);
    }

    @Override
    public Result update(Dish dish) {
        dish.setUpdateTime(LocalDateTime.now());
        // update dish set ...... where dish_id = ?
        int rows = dishMapper.updateById(dish);
        if(rows >0)
            return Result.updateSuccess(dish);
        return Result.updateFail(dish);
    }

    @Override
    public Result delete(Integer dishId) {
        // delete from dish where dish_id = ?
        int rows = dishMapper.deleteById(dishId);
        if(rows >0)
            return  Result.deleteSuccess(dishId);
        return Result.deleteFail(dishId);
    }

    @Override
    public Result getByName(String dishName) {
        QueryWrapper<Dish> wrapper = new QueryWrapper<>();
        wrapper.like("dish_Name",dishName);
        List<Dish> dishList = dishMapper.selectList(wrapper);
        if(dishList !=null && dishList.size() >0)
            return Result.getSuccess(dishList);
        else
            return Result.getFail("今はまだこの名前の商品がありません");
    }
}
